﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using OA.DBUtility;

namespace OA.Manager
{
    /// <summary>
    /// T 仅限与model类,这里写的是最基础的方法,如果需要扩展请继承该类进行方法覆盖
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ManagerParent<T> where T : new()
    {
        private SqlService<T> _sqlService = null;

        public ManagerParent()
        {
            _sqlService = new SqlService<T>();
        }

        #region  Method

        /// <summary>
        /// 插入新记录
        /// </summary>
        /// <param name="t1"></param>
        /// <returns></returns>
        public bool InsertModelInfo(T t1)
        {
            return _sqlService.SaveEntity(t1);
        }

        /// <summary>
        /// 根据打印任务ID获得打印任务信息
        /// </summary>
        /// <param name="taskID"></param>
        /// <returns></returns>
        public T GetModelInfoByID(int ID)
        {
            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("ID", ID);
            return _sqlService.GetSingleByFields(dic, null);
        }
        /// <summary>
        /// 根据自定义的查询条件得到对象
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public T GetModelInfoBySqlString(string filter)
        {
            string sql = "select * from " + SqlUtility.GetMappingTableName(typeof(T));
            if (!string.IsNullOrEmpty(filter))
                sql += " where 1=1  and " + filter;
            return _sqlService.GetSingleBySqlString(sql);
        }

        public List<T> GetSearchAll(string search)
        {
            string sql = "select * from " + SqlUtility.GetMappingTableName(typeof(T));
            if (!string.IsNullOrEmpty(search))
                sql += " where 1=1  " + search;
            return _sqlService.GetAllBySqlString(sql);
        }
        public List<T> GetSearchSqlAll(string sql)
        {
            //string sql = "select * from " + SqlUtility.GetMappingTableName(typeof(T));
            //if (!string.IsNullOrEmpty(search))
            //    sql += " where 1=1  " + search;
            return _sqlService.GetAllBySqlString(sql);
        }

        public DataTable GetPaged(int pageIndex, int pageSize, ref int total)
        {
            return _sqlService.GetPaged("*", "", pageIndex, pageSize, ref pageSize, ref total);
        }

        public DataTable GetSearchPaged(T info, int pageIndex, int pageSize, ref int total)
        {
            return _sqlService.GetSearchPaged(info, pageIndex, pageSize, ref total);
        }

        public DataTable GetSearchPaged(string search, int pageIndex, int pageSize, ref int total)
        {
            return _sqlService.GetPaged("*", search, pageIndex, pageSize, ref pageSize, ref total);
        }
        public DataTable GetSearchPaged(string tableName, string columns, string search, int pageIndex, int pageSize, ref int total)
        {
            return _sqlService.GetPaged(tableName, string.IsNullOrEmpty(columns) ? "*" : columns, search, pageIndex, pageSize, ref pageSize, ref total);
        }

        /// <summary>
        /// 判断该打印任务是否存在
        /// </summary>
        /// <param name="t1"></param>
        /// <param name="t1"> </param>
        /// <returns></returns>
        public bool IsExistModelInfo(T t1)
        {
            return _sqlService.IsExist(t1);
        }

        /// <summary>
        /// 根据指定的字段更新，字段名是数据库中对应的名称
        /// </summary>
        /// <param name="t1"></param>
        /// <param name="needUpdateFields"></param>
        /// <returns></returns>
        public bool UpdateModelInfo(T t1, List<string> needUpdateFields)
        {
            return _sqlService.UpdateByFields(t1, needUpdateFields);
        }

        /// <summary>
        /// 删除该检查任务
        /// </summary>
        /// <param name="t1"></param>
        /// <returns></returns>
        public bool DeleteModelInfo(T t1)
        {
            return _sqlService.DeleteEntity(t1);
        }

        #endregion  Method
    }
}
